import axios from "axios";

const CHAT_LIST = "CHAT_LIST";

const initState = {
  chatUserList: []
};

export function chatUser (state = initState, action) {
  switch (action.type) {
    case CHAT_LIST:
      return {...state, chatUserList: action.payload};
    default:
      return state;
  }
}

/*
* action create
* */
function chatList (data) {
  return {
    payload: data,
    type: CHAT_LIST
  };
}


/*
* action
* */
export function getChatList (role) {
  return dispatch => {
    axios.get(`/user/list?role=${role}`)
      .then(res => {
        if (res.status === 200 && res.data.code === 0) {
          dispatch(chatList(res.data.data));
        } else {
          // dispatch();
          console.error(res.data.msg);
        }
      })
      .catch(e => {
        console.error(e.toLocaleString());
        //  dispatch();
      });
  };
}

